package ServletDemo;
/**
 * 每个5s刷新一次页面，获取当前时间
 */

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

public class DateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=GB2312"); //设置响应的 MIME类型。
        //获得一个向客户发送数据的输出流：
        PrintWriter out = resp.getWriter();

        out.println("<HTML>");
        out.println("<BODY>");
        resp.setHeader("refresh", "5;http://www.baidu.com");//设置 Refresh 的值。两秒刷新页面一次,
        //resp.setHeader("refresh","5;http://www.baidu.com");//设置 Refresh 的值。5秒刷新页面一次,并跳转到指定页面
        out.println("Now Time:");
        out.println("<BR>" + new Date());

        out.println("</BODY>");
        out.println("</HTML>");
    }
}
